//权限校验相关操作js
import dayjs from "dayjs"
import store from '@/store/index'

export default{
	
	//缓存 存储key value
	setCacheData(key,val){
		uni.setStorage({
		    key: key,
		    data: val,
		    success: function () {
		        
		    }
		});
	},
	formatDate(date, fmt = 'YYYY-MM-DD') {
		if(date==null){
			return "未填写"
		}else{
			return dayjs(date).format(fmt)
		}
	},
	toast(title,icon,mask,duration,position){
		uni.showToast({
			title: title,
			icon:icon||"none",
			mask:mask||false,
			duration: duration||2000,
			position:position||"center"
		});
	},
	//获取缓存数据信息
	getCacheData(key){
		uni.getStorage({
		    key: key,
		    success: function (res) {
				console.log(res.data)
		        return res.data
		    }
		});
	},
	getStorageSync(key){
		try {
		    const value = uni.getStorageSync(key);
		    if (value) {
		        console.log(value)
				return value;
		    }
		} catch (e) {
		    // error
		}
	},
	removeStorageSync(key){
		try {
		    uni.removeStorageSync(key);
		} catch (e) {
		    // error
		}
	},
	//请求网络获取信息数据
	request(url,obj,method,userType,token){
		let _this = this;
		 return new Promise((resolve, reject) => {
			 let header={
			      'content-type': 'application/json',
			 }
			 if(token){
				 header.authorization = this.getStorageSync("authorization")
			 }
			 uni.request({
				url: url, //仅为示例，并非真实的接口地址
				header:header,
				method:method,
				data:obj,
				success (res) {
					if(res.header['authorization']){
						_this.setCacheData("authorization",res.header['authorization'])
					}
					console.log(res)
					if(res.statusCode==200){
						if(res.data.code==0){
						   resolve(res.data)
						}else if(res.data.code==604){
							//清除token 返回登陆页
							store.dispatch("user/loginOut")
							reject(res.data)
						}else{
						   reject(res.data)
						}	
					}else if(res.statusCode==601){
						//清除token 返回登陆页
						store.dispatch("user/loginOut")
						reject(res.data)
					}else if(res.statusCode==500){
						if(res.data.status==601){
							//清除token 返回登陆页
							store.dispatch("user/loginOut")
							reject(res.data)
						}
					}
				},
				fail(res) {
				    reject(res)
				}
			})
		})
	},
	uploadFile(url,file,data,userType,token){
		let _this = this;
		return new Promise((resolve, reject) => {
			let header = {}
			if(token){
				 header.authorization = this.getStorageSync("authorization")
			}
			uni.uploadFile({
			    url: url, //仅为示例，非真实的接口地址
			    filePath: file,
				header:header,
			    name: 'file',
			    formData:data,
			    success: (res) => {
					if(res.statusCode==200){
						resolve(JSON.parse(res.data))	
					}
			    },fail(error) {
					console.log(error)
			    	reject(error)
			    }
			});
		})
	},
	
	//脱敏操作方法
	hideCode(str, frontLen, endLen) {
	  let len = str.length - frontLen - endLen;
	  let code = '';
	  for (let i = 0; i < len; i++) {
	    code += '*';
	  }
	  return str.substring(0, frontLen) + code + str.substring(str.length - endLen);
	},
	
	goHome() {
		let roleCode = JSON.parse(this.getStorageSync("userInfo")).roleCode
		if(roleCode == "XZ-ADMIN" || roleCode == "XZ-YH"){
			//消灾企业用户
			uni.redirectTo({
				url:"/pages/index/index_xz"
			})
		} else if(roleCode == "WB-ADMIN" || roleCode == "WB-YH"){
			//维保企业用户
			uni.redirectTo({
				url:"/pages/index/index_wb"
			})
		}else if(roleCode == "JC-ADMIN" || roleCode == "JC-YH"){
				//维保企业用户
				uni.redirectTo({
					url:"/pages/index/index_jc"
				})
		}else{
			//其他企业用户
			uni.redirectTo({
				url:"/pages/index/index_qt"
			})
		}
	}
}